/***************************************************************************
 *   This file is part of KTagger                                          *
 *   Copyright (C) 2010 by Luca Bellonda                                   *
 *   lbellonda _at_ gmail.com                                              *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#ifndef DATABASEPRIVATE_H
#define DATABASEPRIVATE_H

#include "database.h"
#include "log.h"
#include <QSqlDatabase>
#include <QHash>

class DatabasePersistence::Private
{

public:
    Private(DatabasePersistence *parent);
    ~Private();

    bool init();
    void setError(const EErrorCode errorCode, const QString &errorMessage);
    EErrorCode getErrorCode();

    bool insertProfile(TagProfile *profile);
    bool updateProfile(TagProfile *profile);
    QVector<TagProfile*> *getProfileList();
    bool saveCurrentProfile(TagProfile *profile);
    bool getCurrentProfileId(QString &result);
    bool saveTagMRU(const QString &tag);
    bool saveTagSetConfiguration(const QString &tags);
    QStringList getLastUsedTags();
    QStringList getCompanionTags(const QString &tag);
    QVector<TagProfile*> *getProfileListLight();
    QStringList getProfileTags(const QString & id, bool &isOk);
    TagProfile* getProfile(const QString &id);
    QStringList getActivityTags(const QString & activityName, bool &isOk);

private:
    LogParam  logParam;
    bool _isInited;
    QSqlDatabase _db;
    QString _errorMessage;
    EErrorCode _errorCode;
    int     _dbErrorCode;
    QString _dbErrorText;
    DatabasePersistence *_parent;

    bool initDB();
    bool createTables();
    void setError();
    void initLogger();
    void readAProfile(QSqlQuery &query, TagProfile *profile);
    bool getProfilesActivities(QHash<QString, TagProfile*> &profilesCollection);
    bool getProfilesTags(QHash<QString, TagProfile*> &profilesCollection);
    bool insertProfileTags(QSqlQuery &query, TagProfile *profile);
    bool insertProfileActivities(QSqlQuery &query, TagProfile *profile);
    QVector<TagProfile*> *getProfilesEntities(const bool retrieveCompleteProfile);

};

#endif // DATABASEPRIVATE_H
